package com.example.taobaolianmeng.presenter.impl;

import android.annotation.SuppressLint;
import android.util.Log;

import com.example.taobaolianmeng.model.Api;
import com.example.taobaolianmeng.model.domain.Categories;
import com.example.taobaolianmeng.model.domain.SelectedContent;
import com.example.taobaolianmeng.model.domain.SelectedPageCategory;
import com.example.taobaolianmeng.presenter.ISelectedPagePresenter;
import com.example.taobaolianmeng.utils.RetrofitManager;
import com.example.taobaolianmeng.utils.UrlUtils;
import com.example.taobaolianmeng.view.ISelectedPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectedPagePresenterImpl implements ISelectedPagePresenter {
    private static final String TAG = "SelectedPagePresenterImpl";
    private ISelectedPageCallback mViewCallback = null;
    private final Api mApi;
    private SelectedPageCategory.DataBean mCurrentCategoryItem = null;

    public SelectedPagePresenterImpl(){
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getCategories() {
        if (mViewCallback !=null) {
            mViewCallback.onLoading();
        }
        //拿到retrofit
        Call<SelectedPageCategory> task = mApi.getSelectedPageCategories();
        task.enqueue(new Callback<SelectedPageCategory>() {
            @SuppressLint("LongLogTag")
            @Override
            public void onResponse(Call<SelectedPageCategory> call, Response<SelectedPageCategory> response) {
                int code = response.code();
                if(code == HttpURLConnection.HTTP_OK){
                    SelectedPageCategory result = response.body();
                    Log.d(TAG,"res->"+result);
                    //ui更新
                    if (mViewCallback != null) {
                        mViewCallback.onCategoriesLoaded(result);
                    }
                }else{
                    onLoadError();
                }
            }

            @Override
            public void onFailure(Call<SelectedPageCategory> call, Throwable t) {
                onLoadError();
            }
        });

    }

    private void onLoadError() {
        if (mViewCallback != null) {
            mViewCallback.onNetworkError();
        }
    }

    @SuppressLint("LongLogTag")
    @Override
    public void getContentByCategory(SelectedPageCategory.DataBean item) {
        this.mCurrentCategoryItem = item;
        int categoryId = item.getFavorites_id();
        String targetUrl = UrlUtils.getSelectedPageContentUrl(categoryId);
//        Log.d(TAG,"targetUrl->"+targetUrl);
        Call<SelectedContent> task = mApi.getSelectedPageContent(targetUrl);
        task.enqueue(new Callback<SelectedContent>() {
            @SuppressLint("LongLogTag")
            @Override
            public void onResponse(Call<SelectedContent> call, Response<SelectedContent> response) {
                int code = response.code();
                if(code == HttpURLConnection.HTTP_OK){
                    SelectedContent result = response.body();
//                    Log.d(TAG,"result->"+result);
                    //ui更新
                    if (mViewCallback != null) {
                        mViewCallback.onContentLoaded(result);
                    }
                }else{
                    onLoadError();
                }
            }

            @Override
            public void onFailure(Call<SelectedContent> call, Throwable t) {
                onLoadError();
            }
        });
    }

    @Override
    public void reloadContent() {
        this.getCategories();
    }

    @Override
    public void registerViewCallback(ISelectedPageCallback callBack) {
        this.mViewCallback = callBack;
    }

    @Override
    public void unregisterViewCallback(ISelectedPageCallback callBack) {
        this.mViewCallback = null;
    }
}
